package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RedisDemoZset {
    public static void test1(Jedis jedis){
        System.out.println("zadd zrange Tuple");
        jedis.flushAll();

        jedis.zadd("key",10.0,"zhangsan");
        Map<String,Double> map = new HashMap<>();
        map.put("lisi",20.0);
        map.put("wangwu",30.0);
        jedis.zadd("key",map);

        List<String> ret = jedis.zrange("key",0,-1);
        System.out.println("mumbers :"+ret);

        List<Tuple> rest = jedis.zrangeWithScores("key",0,-1);
        System.out.println("mumbers with score:"+rest);

        String member = rest.get(0).getElement();
        Double score = rest.get(0).getScore();
        System.out.println("member + score:"+member+" + "+score);
    }
    public static void  test2(Jedis jedis){
        System.out.println("zcard ");
        jedis.flushAll();

        jedis.zadd("key",10.0,"zhangsan");
        jedis.zadd("key",20.0,"lisi");
        jedis.zadd("key",30.0,"wangwu");

        long len = jedis.zcard("key");
        System.out.println(len);
    }

    public static void test3(Jedis jedis){
        System.out.println("zrem ");
        jedis.flushAll();

        jedis.zadd("key",10.0,"zhangsan");
        jedis.zadd("key",20.0,"lisi");
        jedis.zadd("key",30.0,"wangwu");

        List<Tuple> rest = jedis.zrangeWithScores("key",0,-1);
        System.out.println("mumbers with score:"+rest);

        long len = jedis.zrem("key","zhangsan","lisi");
        System.out.println("srem n:"+len);

        rest = jedis.zrangeWithScores("key",0,-1);
        System.out.println("mumbers with score:"+rest);

    }
    public static void test4(Jedis jedis){
        System.out.println("zscore");
        jedis.flushAll();

        jedis.zadd("key",10.0,"zhangsan");
        jedis.zadd("key",20.0,"lisi");
        jedis.zadd("key",30.0,"wangwu");

        Double score = jedis.zscore("key","zhangsan");
        System.out.println("score : "+score);

        score = jedis.zscore("key","zhangsan2");
        System.out.println("score : "+score);
    }
    public static void test5(Jedis jedis){
        System.out.println("zrank 返回有序集中指定成员的排名（从0开始");
        jedis.flushAll();

        jedis.zadd("key",10.0,"zhangsan");
        jedis.zadd("key",20.0,"lisi");
        jedis.zadd("key",30.0,"wangwu");

        Long rank = jedis.zrank("key","zhangsan");
        System.out.println("zhangsan rank:"+rank);
        rank = jedis.zrank("key","lisi");
        System.out.println("lisi rank:"+rank);
        rank = jedis.zrank("key","wangwu");
        System.out.println("wangwu rank:"+rank);
        rank = jedis.zrank("key","wangi");
        System.out.println("wangi rank:"+rank);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            //test1(jedis);
            //test2(jedis);
            //test3(jedis);
            //test4(jedis);
            test5(jedis);
        }
    }
}
